<?
/*
Plugin Name: laut.fm-station-feed
Plugin URI: http://www.wuerzblog.de/radio/
Description: Displays the "now playing" of a laut.fm-station
Author: Ralf Thees
Version: 0.1
License: GPL
Author URI: http://wuerzblog.de
Min WP Version: 1.5
Max WP Version: 2.7.x
*/


function lfm_install() {
$default_pattern='<img src="%bandbildurl%" /><br />Wer: %band%<br />Was: %titel%<br />Aus dem Jahr %jahr%';
	if (!get_option("lfm_pattern")) {
		update_option("lfm_pattern",$default_pattern);
	}
}

function laut_fm_station_feed() {



function getNodeValue($nodename,$array) {
	foreach ($array as $key=>$val) {

	        if ($val["tag"] == $nodename && $val["type"] == "complete") {
			$temp=$val["value"];
		}
	}
	return $temp;
}

$station = get_option('lfm_station');
$lfm_pattern=get_option("lfm_pattern");
$http_opts = array('http' =>
    array(
        'timeout'  => '5'
    )
);
$stream_context=stream_context_create($http_opts);
$xmlinput = @file_get_contents('http://www.laut.fm/user/'.$station.'/jetzt.xml',false,$stream_context);
$xml = xml_parser_create();
xml_parse_into_struct($xml, $xmlinput, $dataArray);
xml_parser_free($xml);

$lfmdata["name"]= getNodeValue("NAME",$dataArray);
$lfmdata["title"]= getNodeValue("TITLE",$dataArray);
$lfmdata["year"]= getNodeValue("YEAR",$dataArray);
$lfmdata["record"]= getNodeValue("RECORD",$dataArray);
$lfmdata["url"]= getNodeValue("URL",$dataArray);
$lfmdata["image"]= getNodeValue("IMAGE",$dataArray);
$lfmdata["teaser"]= getNodeValue("TEASER",$dataArray);

$lfm_varname["name"]= "/%band%/";
$lfm_varname["title"]= "/%titel%/";
$lfm_varname["year"]= "/%jahr%/";
$lfm_varname["record"]= "/%album%/";
$lfm_varname["url"]= "/%infourl%/";
$lfm_varname["image"]= "/%bandbildurl%/";
$lfm_varname["teaser"]= "/%teaser%/";



$lfm_out=preg_replace($lfm_varname,$lfmdata,$lfm_pattern);


$wppath = get_bloginfo('wpurl');
echo $lfm_out;
}



// Hook for adding an admin menu entry for configuration page:
add_action('admin_menu', 'lautfm_station_feed');

register_activation_hook(__FILE__,'lfm_install');





function lautfm_station_feed() {
    add_options_page('Laut.fm', 'Laut.fm', 8, 'lautfm-stationfeed.php', 'lautfm_options_page');
}


function lautfm_options_page() {
    _e ("<h2>Laut.fm Einstellungen</h2>");

    $opt_name = 'lfm_station';
    $hidden_field_name = 'submit_hidden';
    $data_field_name = 'lfm_station';

    $opt_val = get_option( $opt_name );
	$lfm_pattern=get_option("lfm_pattern");

    if( $_POST[ $hidden_field_name ] == 'Y' ) {
        $opt_val = $_POST[ $data_field_name ];
        update_option( $opt_name, $opt_val );
	update_option("lfm_pattern",$_POST["lfm_pattern"]);

?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php

    }


    echo '<div class="wrap">';



    echo "<h2>" . __( 'Laut.fm Einstellungen', 'mt_trans_domain' ) . "</h2>";


    
    ?>

<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">

<p><?php _e("Stationsname:", 'mt_trans_domain' ); ?> 
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
</p>
<p><?php _e("Ausgabe:", 'mt_trans_domain' ); ?> 
<textarea name="lfm_pattern" cols="50" rows="10" ><?php echo $lfm_pattern; ?></textarea>
</p>
<hr />

<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'mt_trans_domain' ) ?>" />
</p>

</form>
</div>

<?php
 
}
?>
